package com.gexiaobao.pigeon.ui.fragment.match

//import kotlinx.android.synthetic.main.fragment_insert_group.swipeRefreshLayout
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.core.content.ContextCompat
import com.bin.david.form.core.SmartTable
import com.bin.david.form.core.TableConfig
import com.bin.david.form.data.CellInfo
import com.bin.david.form.data.format.bg.BaseCellBackgroundFormat
import com.bin.david.form.data.style.FontStyle
import com.bin.david.form.data.style.LineStyle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.InsertGroupResponse
import com.gexiaobao.pigeon.databinding.FragmentInsertGroupBinding
import com.gexiaobao.pigeon.ui.race.RaceViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/1 10:49
 * @Describe    : 插组
 */
class FragmentInsertGroup : BaseFragment<RaceViewModel, FragmentInsertGroupBinding>() {

    private var raceId = ""
    private var racePassId = ""
    lateinit var data: SmartTable<InsertGroupResponse.DataInfo>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeToolbarMenu.toolbar.initClose("插组") {
            nav().navigateUp()
        }
        mDatabind.includeToolbarMenu.tvRightMenu.text = "刷新"
        raceId = arguments?.getInt("raceId").toString()
        racePassId = arguments?.getInt("racingPassId").toString()
        initSmartTable()
    }

    private fun initSmartTable() {
        data = mDatabind.smartTable as SmartTable<InsertGroupResponse.DataInfo>
        val fontStyle = context?.let { ContextCompat.getColor(it, R.color.black) }
            ?.let { FontStyle(context, 18, it) }
        val contentStyle = context?.let { ContextCompat.getColor(it, R.color.comm_text_gray) }
            ?.let { FontStyle(context, 16, it) }
        val lineStyle = LineStyle()
        lineStyle.color = context?.let { ContextCompat.getColor(it, R.color.dedede) }!!
        data.config.columnTitleStyle = fontStyle
        data.config.contentStyle = contentStyle
        data.config.setHorizontalPadding(20)
            .setVerticalPadding(40)
            .setSequenceHorizontalPadding(20)
            .setSequenceVerticalPadding(40)
            .setShowTableTitle(false) // 去除标题
            .setFixedFirstColumn(true) //第一行作废
            .setShowXSequence(false)
            .setShowYSequence(false)
            .isFixedYSequence = true
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        showLoading(getString(R.string.loading))
        mViewModel.getGroupProject(raceId, racePassId, "")
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.groupResult.observe(viewLifecycleOwner) {
//            if (swipeRefreshLayout.isRefreshing) {
//                swipeRefreshLayout.isRefreshing = false
//            }
            dismissLoading()
            if (it?.data == null || it.data.size == 0) {
                mDatabind.tvInsertNoData.visibility = View.VISIBLE
                mDatabind.smartTable.visibility = View.GONE
                return@observe
            }
            mDatabind.tvInsertNoData.visibility = View.GONE
            setSmartTableData(it)
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeToolbarMenu.tvRightMenu) {
            when (it) {
                mDatabind.includeToolbarMenu.tvRightMenu -> {
                    showLoading(getString(R.string.loading))
                    mViewModel.getGroupProject(raceId, racePassId, "")
                }
            }
        }
    }

    private fun setSmartTableData(it: InsertGroupResponse?) {
        val listData = ArrayList<InsertGroupResponse.DataInfo>()
        if (it != null) {
            if (it.data.isNotEmpty()) {
                it.data.forEachIndexed { index, dataInfo ->
                    listData.add(index, dataInfo)
                    dataInfo.orderNumber = index + 1
                    val pass = dataInfo.aboutPass
                    if (pass.contains(",")) {
                        val str = pass.split(",")
                        if (str.isNotEmpty()) {
                            val sb = StringBuilder()
                            for (i in str.indices) {
                                val result = sb.append(str[i] + "\n")
                                dataInfo.aboutPass = result.toString()
                            }
                        }
                    }
                }
                data.setData(listData)

                data.config.contentCellBackgroundFormat =
                    object : BaseCellBackgroundFormat<CellInfo<*>>() {
                        override fun getBackGroundColor(t: CellInfo<*>): Int {
                            return TableConfig.INVALID_COLOR
                        }

                        override fun getTextColor(t: CellInfo<*>): Int {
                            return if ("查看成绩" == t.column.columnName || "查看报名" == t.column.columnName) {
                                ContextCompat.getColor(context!!, R.color.bg_light_bllue)
                            } else {
                                super.getTextColor(t)
                            }
                        }
                    }
                data.tableData?.setOnItemClickListener { column, value, t, col, row ->
                    if ("查看成绩" == column.columnName) {
                        if (t != null) {
                            if (!TextUtils.isEmpty(t.toString())) {
                                nav().navigateAction(R.id.action_to_look_result, Bundle().apply {
                                    putInt("id", listData[row].id)
                                })
                            }
                        }
                    }

                    if ("查看报名" == column.columnName) {
                        if (t != null) {
                            if (!TextUtils.isEmpty(t.toString())) {
                                nav().navigateAction(R.id.action_to_look_sign_up, Bundle().apply {
                                    putInt("id", listData[row].id)
                                })
                            }
                        }
                    }
                }
            }
        }
    }
}